import logging
import os
import time

from pandas._testing import loc
from selenium.webdriver.support.wait import WebDriverWait

from base import log
from config import basic_log_config
from util import DriverUtils


class BuyerBasePage:
    def __init__(self):
        self.driver=DriverUtils.get_buyer_driver()


    def find_el(self,location):
        try:
            # 显示等待的方式定位元素
            el=WebDriverWait(self.driver,10,1).\
                until(lambda x:x.find_element(*location))
            logging.info(f"excute find_el {location} success!")
        except Exception as e:
            logging.error(f"excute find_el {location} failed!")
            el=None
            # 返回元素对象
        return el


    def input_text(self,el,key_text):
        """
        :param el: 元素对象
        :param key_text: 输入的文本
        :return:
        """
        try:
            # 模拟清楚
            el.clear()
            logging.info(f"excute clear {el}'s text success!")
            # 模拟输入
            el.sendkeys(key_text)
            logging.info(f"excute input {el}'s {key_text} success!")
        except Exception as e:
            logging.error(f"excute input {el}'s {key_text} failed!")
    # 窗口切换
    def switch_window(self,n):
        """
        :param n: 切换到第几个窗口，整型
        :return: None
        """
        try:
            # 获取句柄
            handles=self.driver.window_handles
            # 切换窗口
            self.driver.switch_to.window(handles[n])
            # 打印日志
            logging.info(f"switch to {n} window success")
        except Exception as e:
            logging.error(f"switch to {n} window failed")
    def switch_frame(self,el):
        """
        :param el:  <iframe>标签的元素对象
        :return: None
        """
        try:
           #切换指定<iframe>标签的元素对象
            self.driver.switch_to.frame(el)
           # 打印日志
            logging.info(f"switch to {el} iframe success")
        except Exception as e:
            logging.info(f"switch to {el} iframe failed")

class AdminBasePage:
    def __init__(self):
        self.driver = DriverUtils.get_admin_driver()

    def find_el(self, location):
        try:
            # 显示等待的方式定位元素
            el = WebDriverWait(self.driver, 10, 1). \
                until(lambda x: x.find_element(*location))
            logging.info(f"excute find_el {location} success!")
        except Exception as e:
            logging.error(f"excute find_el {location} failed!")
            el = None
            # 返回元素对象
        return el

    def input_text(self, el, key_text):
        """
        :param el: 元素对象
        :param key_text: 输入的文本
        :return:
        """
        try:
            # 模拟清楚
            el.clear()
            logging.info(f"excute clear {el}'s text success!")
            # 模拟输入
            el.sendkeys(key_text)
            logging.info(f"excute input {el}'s {key_text} success!")
        except Exception as e:
            logging.error(f"excute input {el}'s {key_text} failed!")
        # 窗口切换

    def switch_window(self, n):
        """
        :param n: 切换到第几个窗口，整型
        :return: None
        """
        try:
            # 获取句柄
            handles = self.driver.window_handles
            # 切换窗口
            self.driver.switch_to.window(handles[n])
            # 打印日志
            logging.info(f"switch to {n} window success")
        except Exception as e:
            logging.error(f"switch to {n} window failed")

    def switch_frame(self, el):
        """
        :param el:  <iframe>标签的元素对象
        :return: None
        """
        try:
            # 切换指定<iframe>标签的元素对象
            self.driver.switch_to.frame(el)
            # 打印日志
            logging.info(f"switch to {el} iframe success")
        except Exception as e:
            logging.info(f"switch to {el} iframe failed")


class AppBasePage:
    def __init__(self):
        self.driver = DriverUtils.get_app_driver()

    def find_el(self, location, timeout=10, poll=0.5):
     log.info("正在查找元素：{}".format(location))
     return WebDriverWait(self.driver, timeout, poll).until(lambda x: x.find_element(*location))
    # def find_el(self, location):
    # def input_text(self, el, key_text):
    #
    # def switch_window(self, n):
    #
    # def switch_frame(self, el):
    #   log.info("正在调用切换frame方法，切换对象：{}".format(el))
    # # 获取元素
    #   el = self.base_find(el)
    # # 执行切换
    #   self.driver.switch_to.frame(el)
# class Base:
#
#     # 初始化方法
#     def __init__(self, driver):
#         log.info("正在初始化，driver对象：{}".format(driver))
#         self.driver = driver
#
#     # 查找元素方法
#     def base_find(self, loc, timeout=10, poll=0.5):
#         log.info("正在查找元素：{}".format(loc))
#         return WebDriverWait(self.driver, timeout, poll).until(lambda x: x.find_element(*loc))
#
#     # 点击方法
#     def base_click(self, loc):
#         log.info("正调用点击元素：{}".format(loc))
#         self.base_find(loc).click()
#
#     # 输入方法
#     def base_input(self, loc, value):
#         log.info("正调用输入元素方法：{} 输入内容：{}".format(loc,value))
#         # 获取元素
#         el = self.base_find(loc)
#         # 清空
#         el.clear()
#         # 输入
#         el.send_keys(value)
#
#     # 获取文本方法
#     def base_get_text(self, loc):
#         log.info("正调用获取元素信息方法：{}".format(loc))
#         return self.base_find(loc).text
#
#     # 截图方法
#     def base_get_img(self):
#         log.info("正调用截图方法")
#         img_path = DIR_PATH + os.sep + "img" + os.sep + "{}.png".format(time.strftime("%Y%m%d%H%M%S"))
#         self.driver.get_screenshot_as_file(img_path)

    # 切换frame
    # def switch_frame(self, loc):
    #     log.info("正在调用切换frame方法，切换对象：{}".format(loc))
    #     # 获取元素
    #     el = self.base_find(loc)
    #     # 执行切换
    #     self.driver.switch_to.frame(el)
#
#     # 恢复frame
#     def base_default_frame(self):
#         self.driver.switch_to.default_content()
